package cc.wanforme.st.server.base.service.impl;

import cc.wanforme.st.server.base.entity.Role;
import cc.wanforme.st.server.base.mapper.RoleMapper;
import cc.wanforme.st.server.base.service.IRoleService;
import cc.wanforme.st.server.exception.MunkRuntimeException;
import cc.wanforme.st.server.util.AssertCheck;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wanne
 * @since 2022-03-03
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

	@Override
	public void saveVo(Role role) {
		AssertCheck.nonNull(role.getRoleCode(), "角色代码不能为空");
		AssertCheck.nonNull(role.getName(), "角色名不能为空");
		
		Role existed = this.getById(role.getRoleCode());
		if(existed != null) {
			throw new MunkRuntimeException("角色代码重复!");
		}
		
		this.save(role);
	}

	@Override
	public void updateVo(Role role) {
		AssertCheck.nonNull(role.getName(), "角色名不能为空");
		this.updateById(role);
	}

	@Override
	public void deleteVo(String roleCode) {
		this.baseMapper.deleteById(roleCode);
	}

	@Override
	public PageInfo<Role> searchRoles(int page, int pageSize, @Nullable String name) {
		PageHelper.startPage(page, pageSize);
		List<Role> list = this.baseMapper.listByName(name);
		return new PageInfo<>(list);
	}

}
